package org.darkforest.common.services.impl;

import org.darkforest.common.services.MansongService;
import org.darkforest.common.util.DateUtil;
import org.darkforest.dao.mapper.PMansongMapper;
import org.darkforest.dao.mapper.PMansongRuleMapper;
import org.darkforest.model.PMansong;
import org.darkforest.model.PMansongRule;
import org.darkforest.vo.AjaxReturnData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 *
 * Created by Administrator on 2016/1/12.
 */
@Service
@Transactional
public class MansongServiceImpl implements MansongService {

    @Autowired
    private PMansongMapper pMansongMapper;

    @Autowired
    private PMansongRuleMapper pMansongRuleMapper;



    @Override
    public int deleteByPrimaryKey(Integer mansongId) {
        return pMansongMapper.deleteByPrimaryKey(mansongId);
    }

    @Override
    public int insert(PMansong record) {
        return pMansongMapper.insert(record);
    }

    @Override
    public int insertSelective(PMansong record) {
        return pMansongMapper.insertSelective(record);
    }

    @Override
    public PMansong selectByPrimaryKey(Integer mansongId) {
        return pMansongMapper.selectByPrimaryKey(mansongId);
    }

    @Override
    public int updateByPrimaryKeySelective(PMansong record) {
        return pMansongMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(PMansong record) {
        return pMansongMapper.updateByPrimaryKey(record);
    }

    /**
     * 根据传入的查询条件查询满即送活动列表
     *
     * @param record
     * @return List<PMansong>
     */
    @Override
    public List<PMansong> findListByCondition(PMansong record) {
        return pMansongMapper.findListByCondition(record);
    }

    @Override
    public List<PMansong> findListWithRuleByCondition(PMansong record) {
        return pMansongMapper.findListWithRuleByCondition(record);
    }

    /**
     * 根据满送Id查询满送活动以及满送活动对应规则
     *
     * @param mansongId
     * @return PMansong
     */
    @Override
    public PMansong getPMansongWithRules(Integer mansongId) {
        return pMansongMapper.getPMansongWithRules(mansongId);
    }

    /**
     * 添加满送活动 ajax返回
     *
     * @param pMansong
     * @return
     */
    @Override
    public AjaxReturnData addManSongActive(PMansong pMansong,String url) throws Exception {
        StringBuilder stringBuilder = new StringBuilder();
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        try{
            pMansong.setStartTime(DateUtil.toUnixTimeStamp(pMansong.getStartTimeDes()));
            pMansong.setEndTime(DateUtil.toUnixTimeStamp(pMansong.getEndTimeDes()));
            pMansong.setQuotaId(0);
            pMansongMapper.insertSelective(pMansong);
            for(String rules:pMansong.getManSongRule()){
                String[] rule = rules.split(",");
                PMansongRule pMansongRule = new PMansongRule();
                pMansongRule.setMansongId(pMansong.getMansongId());
                pMansongRule.setPrice(org.apache.commons.lang.StringUtils.isEmpty(rule[0]) ? null : Integer.parseInt(rule[0]));
                pMansongRule.setDiscount(org.apache.commons.lang.StringUtils.isEmpty(rule[1]) ? null : Integer.parseInt(rule[1]));
                pMansongRule.setGoodsId(org.apache.commons.lang.StringUtils.isEmpty(rule[2]) ? null : Integer.parseInt(rule[2]));
                pMansongRule.setMansongGoodsName(org.apache.commons.lang.StringUtils.isEmpty(rule[3]) ? null : rule[3]);

//                stringBuilder.append("满" + pMansongRule.getPrice() + "减" + pMansongRule.getDiscount());
//                if(pMansongRule.getGoodsId()!=null){
//                    stringBuilder.append("送"+pMansongRule.getMansongGoodsName());
//                }
//
//

                pMansongRuleMapper.insertSelective(pMansongRule);
            }

            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("新增满即送活动成功");
            ajaxReturnData.setUrl(url);
        }catch (Exception e){
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("Exception : 新增满即送活动失败");
            throw  new Exception("新增满即送活动失败"+e);
        }

        return ajaxReturnData;
    }

    /**
     * 删除满送活动 ajax返回
     *
     * @param mansongId
     * @return AjaxReturnData
     */
    @Override
    public AjaxReturnData deleMansongActive(Integer mansongId,String url) throws Exception {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();

        try{
            pMansongMapper.deleteByPrimaryKey(mansongId);
            pMansongRuleMapper.deleteByMansongId(mansongId);
            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("删除满即送活动成功");
            ajaxReturnData.setUrl(url);
        }catch (Exception e){
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("删除满即送活动失败");
            throw new Exception("删除满即送活动失败"+e);
        }




        return ajaxReturnData;
    }

    /**
     * 取消满送活动
     *
     * @param mansongId
     * @param state
     * @return
     */
    @Override
    public AjaxReturnData updateMansong(Integer mansongId, byte state,String url) throws Exception {
        AjaxReturnData ajaxReturnData = new AjaxReturnData();
        try{
        PMansong pMansong = new PMansong();
        pMansong.setMansongId(mansongId);
        pMansong.setState(state);
        pMansongMapper.updateByPrimaryKeySelective(pMansong);
            ajaxReturnData.setResult(true);
            ajaxReturnData.setMessage("修改满即送活动状态成功");
            ajaxReturnData.setUrl(url);
        }catch (Exception e){
            ajaxReturnData.setResult(false);
            ajaxReturnData.setMessage("修改满即送活动状态失败");
            throw new Exception("修改满即送活动状态失败"+e);
        }
        return ajaxReturnData;
    }
}
